Skip to content

Add Logger property to ExecuteCommandContext#15678

Open
tranhoangtu-it wants to merge 3 commits intomicrosoft:mainfrom
tranhoangtu-it:add-logger-to-execute-command-context
Open

Add Logger property to ExecuteCommandContext#15678
tranhoangtu-it wants to merge 3 commits intomicrosoft:mainfrom
tranhoangtu-it:add-logger-to-execute-command-context

Conversation

@tranhoangtu-it
Copy link
Copy Markdown

Fixes #15582

Adds a Logger property to ExecuteCommandContext so command handlers can log directly to the resource's log stream without resolving it from ServiceProvider.

The logger instance comes from ResourceLoggerService.GetLogger(resourceId), which is already obtained in ResourceCommandService — this change just passes it through to the context.

Changes

  • ResourceCommandAnnotation.cs — Added required ILogger Logger { get; init; } to ExecuteCommandContext
  • ResourceCommandService.cs — Passes existing logger when constructing the context
  • Aspire.Hosting.cs — Updated API surface file

Copilot AI review requested due to automatic review settings March 28, 2026 16:14
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 28, 2026

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 15678

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 15678"

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a Logger property to ExecuteCommandContext so resource command handlers can log directly to the resource’s log stream without resolving a logger from IServiceProvider.

Changes:

  • Added ILogger Logger to ExecuteCommandContext (public API).
  • Passed the existing resource logger into ExecuteCommandContext from ResourceCommandService.
  • Updated the public API surface file to include the new property.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/Aspire.Hosting/ApplicationModel/ResourceCommandAnnotation.cs Extends ExecuteCommandContext with a Logger property.
src/Aspire.Hosting/ApplicationModel/ResourceCommandService.cs Populates ExecuteCommandContext.Logger using the existing ResourceLoggerService logger.
src/Aspire.Hosting/api/Aspire.Hosting.cs Updates API surface to include the new Logger property.

Comment on lines 245 to +250
public required CancellationToken CancellationToken { get; init; }

/// <summary>
/// The logger for the resource.
/// </summary>
public required ILogger Logger { get; init; }
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding required ILogger Logger makes ExecuteCommandContext source-breaking for any consumers that construct it (e.g., tests) and is inconsistent with other callback contexts that default Logger to NullLogger.Instance. Consider making Logger non-required and initializing it to NullLogger.Instance (and still setting it in ResourceCommandService) so the property is always non-null without forcing callers to provide it.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

@JamesNK JamesNK Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this. It's only a source breaking change and I doubt many people create this type themselves.

Simple enough to fix by assigning a NullLogger.Instance in tests on upgrade to fix.

The alternative is not making it required and assigning it = null!;. Trades a build time error for runtime errors.

Comment on lines 245 to +250
public required CancellationToken CancellationToken { get; init; }

/// <summary>
/// The logger for the resource.
/// </summary>
public required ILogger Logger { get; init; }
Copy link
Copy Markdown
Member

@JamesNK JamesNK Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this. It's only a source breaking change and I doubt many people create this type themselves.

Simple enough to fix by assigning a NullLogger.Instance in tests on upgrade to fix.

The alternative is not making it required and assigning it = null!;. Trades a build time error for runtime errors.

@JamesNK
Copy link
Copy Markdown
Member

JamesNK commented Mar 30, 2026

@sebastienros What's required when adding new API to exported type? Is something special required for ILogger?

@sebastienros
Copy link
Copy Markdown
Contributor

@JamesNK It's not breaking, and ILogger/ILoggerFactory is supported by ATS. (same for IServiceProvider, and CancellationToken btw).

@sebastienros
Copy link
Copy Markdown
Contributor

Here for the build to pass the snapshots need to be regenerated (running the tests locally for instance will do it). This change is surely updating the generated SDKs.

Added Logger getter/setter methods to all 5 language SDK snapshots
(Go, Java, Python, Rust, TypeScript) following the existing pattern
from EnvironmentCallbackContext.Logger.
@tranhoangtu-it tranhoangtu-it force-pushed the add-logger-to-execute-command-context branch from 570c5fb to bbecc63 Compare March 30, 2026 17:29
@tranhoangtu-it
Copy link
Copy Markdown
Author

Updated! Reverted the manual API surface file edit (auto-generated per @davidfowl) and regenerated all 5 language SDK snapshots for the new Logger property. The snapshots follow the existing pattern from EnvironmentCallbackContext.Logger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Logger property to ExecuteCommandContext

5 participants